#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""开始执行，
"""
import configparser
import os
from os import path


class Config:
    def __init__(self):
        self.cfg = configparser.ConfigParser()
        cfg_file_path = path.join(path.abspath('.'), 'cfg.ini')
        self.cfg.read(cfg_file_path, encoding='utf-8-sig')
        # 当问题类型全部为否的时候报错
        has_question_type = False
        for key in self.cfg['file']:
            has_question_type = has_question_type or self.cfg.getboolean('file', key)
        if not has_question_type:
            exit('[' + self.__class__.__name__ + ']' + '没有可以使用的题目类型！')
        # 当诗词内容题目配置为有的时候检查是否有诗词内容相关的题目类型
        if self.cfg.getboolean('file', 'poetry_file'):
            has_poetry_content_question_type = False
            for key in self.cfg['question_type']:
                has_poetry_content_question_type = has_poetry_content_question_type or \
                                                   self.cfg.getboolean('question_type', key)
            if not has_poetry_content_question_type:
                exit('[' + self.__class__.__name__ + ']' + '诗词内容问题中没有可加载的题目类型！')
        # 判断配置中的地址是否存在
        error_msg = ''
        for key in self.cfg['path']:
            if not os.path.exists(self.cfg['path'][key]):
                error_msg = error_msg + '路径配置[path][' + key + ']中的配置值[' + self.cfg['path'][key] + ']不存在！\n'
        if error_msg:
            exit('[' + self.__class__.__name__ + ']' + error_msg)

    @staticmethod
    def instance():
        global inst
        try:
            inst
        except:
            inst = Config()
        return inst
